package com.linjiahao.eduservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.linjiahao.commonutils.ResultMap;
import com.linjiahao.commonutils.orderVo.EduCourseOrder;
import com.linjiahao.eduservice.entity.EduCourse;
import com.linjiahao.eduservice.service.EduCourseService;
import com.linjiahao.eduservice.vo.CourseInfoForm;
import com.linjiahao.eduservice.vo.CoursePublisVo;
import com.linjiahao.eduservice.vo.CourseQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-08-24
 */
@Api(description="课程管理")
@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {

    @Autowired
    private EduCourseService courseService;

    @ApiOperation(value = "新增课程")
    @PostMapping("savecourse")
    public ResultMap saveCourseInfo(@ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
        @RequestBody CourseInfoForm courseInfoForm){
        System.out.println(courseInfoForm.toString());
        String courseId = courseService.saveCourseInfo(courseInfoForm);
        if(!StringUtils.isEmpty(courseId)){
            return ResultMap.ok().data("courseId", courseId);
        }else{
            return ResultMap.error().message("保存失败");
        }
    }

    @ApiOperation(value = "根据ID查询课程")
    @GetMapping("course-info/{id}")
    public ResultMap getById(@ApiParam(name = "id", value = "课程ID", required = true)
        @PathVariable String id){
        CourseInfoForm courseInfoForm = courseService.getCourseInfoFormById(id);
        return ResultMap.ok().data("item", courseInfoForm);
    }

    @ApiOperation(value = "更新课程")
    @PutMapping("update-course-info/{id}")
    public ResultMap updateCourseInfoById(@ApiParam(name = "CourseInfoForm", value = "课程基本信息", required = true)
        @RequestBody CourseInfoForm courseInfoForm, @ApiParam(name = "id", value = "课程ID", required = true)
        @PathVariable String id){
        courseService.updateCourseInfoById(courseInfoForm);
        return ResultMap.ok();
    }

    @ApiOperation(value = "根据ID获取课程发布信息")
    @GetMapping("course-publish-info/{id}")
    public ResultMap getCoursePublishVoById(@ApiParam(name = "id", value = "课程ID", required = true)
        @PathVariable String id){
        CoursePublisVo courseInfoForm = courseService.getCoursePublishVoById(id);
        return ResultMap.ok().data("item", courseInfoForm);
    }

    @ApiOperation(value = "根据id发布课程")
    @PutMapping("publish-course/{id}")
    public ResultMap publishCourseById(@ApiParam(name = "id", value = "课程ID", required = true)
        @PathVariable String id){
        courseService.publishCourseById(id);
        return ResultMap.ok();
    }

    @ApiOperation(value = "分页课程列表")
    @GetMapping("{page}/{limit}")
    public ResultMap pageQuery(@ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page, @ApiParam(name = "limit", value = "每页记录数", required = true)
    @PathVariable Long limit, @ApiParam(name = "courseQuery", value = "查询对象", required = false) CourseQuery courseQuery){

        Page<EduCourse> pageParam = new Page<>(page, limit);
        courseService.pageQuery(pageParam, courseQuery);
        List<EduCourse> records = pageParam.getRecords();
        long total = pageParam.getTotal();
        return  ResultMap.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "根据ID删除课程")
    @DeleteMapping("{id}")
    public ResultMap removeById(@ApiParam(name = "id", value = "课程ID", required = true)
        @PathVariable String id){
        boolean result = courseService.removeCourseById(id);
        if(result){
            return ResultMap.ok();
        }else{
            return ResultMap.error().message("删除失败");
        }
    }

    //根据课程id查询课程信息,订单
    @GetMapping("getDto/{courseId}")
    public EduCourseOrder getCourseInfoDto(@PathVariable String courseId) {
        EduCourse courseInfoForm = courseService.getById(courseId);
        EduCourseOrder courseInfo = new EduCourseOrder();
        BeanUtils.copyProperties(courseInfoForm,courseInfo);
        return courseInfo;
    }

    //增加购买数
    @GetMapping("updateCourse/{courseId}")
    public void updatabuyCount(@PathVariable String courseId){
        EduCourse byId = courseService.getById(courseId);
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(byId.getId());
        eduCourse.setBuyCount(byId.getBuyCount()+1);
        courseService.updateById(eduCourse);
    }

}

